package com.ermao.mymes.support.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ermao.mymes.common.constant.MyMesConstant;
import org.springframework.stereotype.Component;

/**
 * 获取账号信息工具类
 * @author Ermao
 * Date: 2022/5/11 21:59
 */
@Component
public class AccountUtils {

	@ApolloConfig("dev.mymes")
	private Config config;

	/**
	 * 短信参数示例：[{"sms_10":{"url":"sms.tencentcloudapi.com","region":"ap-guangzhou","secretId":"AKIDnMgiV1FCtJSKuEG1VkLgka9RRAcngIdi","secretKey":"pNgiTx95j6B0YonWx70bw0nIXVBajaAT","smsSdkAppId":"140025","templateId":"11897","signName":"Java3y公众号","supplierId":10,"supplierName":"腾讯云"}}]
	 * 邮件参数示例：[{"email_10":{"host":"smtp.qq.com","port":465,"user":"yangshifeng19@qq.com","pass":"qagvhawvmzoxdejf","from":"yangshifeng19@qq.com"}}]
	 */
	public <T> T getAccount(Integer sendAccount, String apolloKey, String prefix, T t) {
		// 邮件参数列表为：（发送方，"emailAccount"，"email_"， mailAccount）
		String accountValues = config.getProperty(apolloKey, MyMesConstant.APOLLO_DEFAULT_VALUE_JSON_ARRAY);
		JSONArray jsonArray = JSON.parseArray(accountValues);
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			Object object = jsonObject.getObject(prefix + sendAccount, t.getClass());
			if (object != null) {
				return (T) object;
			}
		}
		return null;
	}
}
